package com.zmm.myyimixian.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.bean.FenLeiTitleItem;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.db.ShoppingCart;
import com.zmm.myyimixian.util.UIManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wencai2015 on 2016/10/21.
 */

public class MyFLTitleItmeApater extends BaseAdapter {
    private List<ShoppingCart> mList;
    private List<FenLeiTitleItem.GoodsBean> goods;
    private ModifyCountInterface modifyCountInterface;
    private int count = 0;

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }


    public void setShuJuKu(List<ShoppingCart> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void setData(List<FenLeiTitleItem.GoodsBean> goods) {
        this.goods = goods;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return goods.size();
    }

    @Override
    public Object getItem(int position) {
        return goods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fenlei_pull_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        FenLeiTitleItem.GoodsBean goodsBean = goods.get(position);
        Glide.with(parent.getContext()).load(ConfigPath.getLunBoPath(goodsBean.getPromotion_img())).placeholder(R.mipmap.icon_home_item_default).into(viewHolder.flItemSmailIv);
        Glide.with(parent.getContext()).load(ConfigPath.getLunBoPath(goodsBean.getImg())).placeholder(R.mipmap.icon_home_item_default).into(viewHolder.flItemBigIv);
        String name = goodsBean.getName();
        viewHolder.flNameTv.setText(name);
        String sub_name = goodsBean.getSub_name();
        if (sub_name == null || "".equals(sub_name)) {
            viewHolder.flSubNameTv.setVisibility(View.GONE);
        } else {
            viewHolder.flSubNameTv.setVisibility(View.VISIBLE);
            viewHolder.flSubNameTv.setText(sub_name);
        }
        viewHolder.flUnitDescTv.setText(goodsBean.getUnit_desc());
        viewHolder.flItemRmbTv.setText("￥" + goodsBean.getUnit_price());

        //查询数据跟json中的数据相比较控制控件的状态
        if (MyApp.isLogin()&& MyApp.isSJK && mList != null) {
            for (int i = 0; i < mList.size(); i++) {
                ShoppingCart shoppingCart = mList.get(i);
                String title = shoppingCart.getTitle();
                if (title.equals(name)) {
                    viewHolder.flItemJianIv.setVisibility(View.VISIBLE);
                    viewHolder.flItemCountTv.setVisibility(View.VISIBLE);
                    String count1 = shoppingCart.getCount();
                    this.count = Integer.valueOf(count1);
                    viewHolder.flItemCountTv.setText(count1);
                    break;
                }else {
                    viewHolder.flItemCountTv.setText("0");
                    viewHolder.flItemJianIv.setVisibility(View.GONE);
                    viewHolder.flItemCountTv.setVisibility(View.GONE);
                }
            }
        }
//        if (MyApp.isLogin() && MyApp.isSJK) {
//            DataDao.getShoppingCart(name, new FindListener<ShoppingCart>() {
//                @Override
//                public void onSuccess(List<ShoppingCart> list) {
//                    if (list.size() != 0) {
//                        String count = list.get(0).getCount();
//                        viewHolder.flItemJianIv.setVisibility(View.VISIBLE);
//                        viewHolder.flItemCountTv.setVisibility(View.VISIBLE);
//                        viewHolder.flItemCountTv.setText(count);
//                    } else {
//                        viewHolder.flItemCountTv.setText("0");
//                        viewHolder.flItemJianIv.setVisibility(View.GONE);
//                        viewHolder.flItemCountTv.setVisibility(View.GONE);
//                    }
//                }
//
//                @Override
//                public void onError(int i, String s) {
//
//                }
//            });
//        }
        viewHolder.flItemJiaIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyApp.isLogin()) {
                    modifyCountInterface.doIncrease(position, viewHolder.flItemCountTv, viewHolder.flItemJianIv, viewHolder.flItemJiaIv, count);
                } else {
                    UIManager.startLoginActivity(parent.getContext());
                }
            }
        });
        viewHolder.flItemJianIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position, viewHolder.flItemCountTv, viewHolder.flItemJianIv, viewHolder.flItemJiaIv, count);
            }
        });
        return convertView;
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param count         数量
         * @param jian          减控件
         * @param jia           加控件
         */
        public void doIncrease(int position, TextView showCountView, ImageButton jian, ImageButton jia, int count);

        /**
         * 删减操作
         *
         * @param position      子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param jian          减控件
         * @param jia           加控件
         * @param count         数量
         */
        public void doDecrease(int position, TextView showCountView, ImageButton jian, ImageButton jia, int count);
    }

    static class ViewHolder {
        @BindView(R.id.fl_item_smail_iv)
        ImageView flItemSmailIv;
        @BindView(R.id.fl_name_tv)
        TextView flNameTv;
        @BindView(R.id.fl_sub_name_tv)
        TextView flSubNameTv;
        @BindView(R.id.fl_unit_desc_tv)
        TextView flUnitDescTv;
        @BindView(R.id.fl_item_jian_iv)
        ImageButton flItemJianIv;
        @BindView(R.id.fl_item_count_tv)
        TextView flItemCountTv;
        @BindView(R.id.fl_item_jia_iv)
        ImageButton flItemJiaIv;
        @BindView(R.id.fl_item_big_iv)
        ImageView flItemBigIv;
        @BindView(R.id.fl_item_rmb_tv)
        TextView flItemRmbTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
